package com.lvtongka.app.fragment.my

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.lvtongka.app.R
import com.lvtongka.app.base.BaseViewModel
import com.lvtongka.app.common.bean.AccountRequest
import com.lvtongka.app.common.bean.IconContainer
import com.lvtongka.app.fragment.my.bean.BindTicketData
import com.lvtongka.app.fragment.my.bean.BindTicketRequest
import com.lvtongka.app.fragment.my.bean.BindTicketResponse
import com.lvtongka.app.fragment.my.bean.MyInfo
import com.lvtongka.app.fragment.my.bean.MyResponse
import com.lvtongka.app.fragment.my.bean.OrderCountData
import com.lvtongka.app.fragment.my.bean.OrderCountResponse
import com.lvtongka.app.helper.SPHelper
import com.lvtongka.app.helper.SPKConstants
import com.lvtongka.app.http.HttpCallBack
import com.lvtongka.app.http.OkhttpUtils
import com.lvtongka.app.http.common.UrlConstants

class MyViewModel : BaseViewModel() {
    private val _result = MutableLiveData<Boolean>()
    val result: LiveData<Boolean> = _result

    private val _myInfo = MutableLiveData<MyInfo>()
    val myInfo: LiveData<MyInfo> = _myInfo

    // 我的卡包
    private val _listWdkb = MutableLiveData<List<IconContainer>>()
    val listCardPck: LiveData<List<IconContainer>> = _listWdkb
    // 我的券包
    private val _listWdqb = MutableLiveData<List<IconContainer>>()
    val listCouponPck: LiveData<List<IconContainer>> = _listWdqb
    // 常用功能
    private val _listCygn = MutableLiveData<List<IconContainer>>()
    val listCommonUse: LiveData<List<IconContainer>> = _listCygn
    // 扫码绑券 查询
    private val _bindTicket = MutableLiveData<BindTicketData>()
    val bindTicket: LiveData<BindTicketData> = _bindTicket
    private val _bindTicketBp = MutableLiveData<Boolean>()
    val bindTicketBp: LiveData<Boolean> = _bindTicketBp
    private val _bindTicketB = MutableLiveData<Boolean>()
    val bindTicketB: LiveData<Boolean> = _bindTicketB
    // 数量
    private val _orderCountData = MutableLiveData<OrderCountData>()
    val orderCountData: LiveData<OrderCountData> = _orderCountData



    fun initCardPck() {
        val list = mutableListOf(
            IconContainer("卡包", R.mipmap.icon_card_bag, 0),
            IconContainer("绑卡", R.mipmap.icon_bind_card, 0),
            IconContainer("查交易", R.mipmap.icon_query_order, 0))
        _listWdkb.postValue(list)
    }

    fun initCouponPck() {
        val list = mutableListOf(
            IconContainer("券包", R.mipmap.icon_coupon, 0),
            IconContainer("绑券", R.mipmap.icon_bind_coupon, 0),
            IconContainer("用券记录", R.mipmap.icon_coupon_record, 0))
//            IconContainer("优惠券", R.mipmap.icon_coupon_my, 0)
        _listWdqb.postValue(list)
    }

    fun initCommonUse() {
        val list = mutableListOf(
            IconContainer("卡延期", R.mipmap.icon_card_extension, 0),
            IconContainer("查余额", R.mipmap.icon_query_balance, 0),
            IconContainer("卡消费", R.mipmap.icon_card_consumption, 0),
            IconContainer("卡挂失", R.mipmap.icon_card_loss, 0),
            IconContainer("收货地址", R.mipmap.icon_address_my, 0),
            IconContainer("帮助中心", R.mipmap.icon_help_center, 0)
        )
        _listCygn.postValue(list)
    }


    fun getMyInfo() {
        val request = AccountRequest("03")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    // 存储数据
                    SPHelper.putString(SPKConstants.BASIC_INFO, result)
                    val my = Gson().fromJson(result, MyResponse::class.java)
                    when(my.data.cell_bind_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_CELL_BIND, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_CELL_BIND, true)
                    }
                    when(my.data.real_name_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_REAL_NAME, true)
                    }
                    when(my.data.password_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_SET_PWD, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_SET_PWD, true)
                    }
                    when(my.data.func_scan_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_FUNC_SCAN, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_FUNC_SCAN, true)
                    }
                    when(my.data.no_password_flag) {
                        "0" -> SPHelper.putBoolean(SPKConstants.IS_NO_PASSWORD, false)
                        "1" -> SPHelper.putBoolean(SPKConstants.IS_NO_PASSWORD, true)
                    }
                    SPHelper.putInt(SPKConstants.MY_LIMIT_NO_PASSWORD, my.data.limit_no_password)
                    SPHelper.putString(SPKConstants.MY_CELL, my.data.cell)
                    SPHelper.putString(SPKConstants.MEMBER_ID, my.data.member_id)
                    _myInfo.postValue(my.data)
                }
            }, getAccessToken())
    }

    /**
     * 扫码绑券 查询
     */
    fun scanCodeBindBp() {
        val request = AccountRequest("39")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT_COUPON, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _bindTicketBp.postValue(true)
                }
            }, getAccessToken())
    }

    /**
     * 扫码绑券 查询
     */
    fun scanCodeBindTicket(qrCode: String) {
        val request = BindTicketRequest(qrCode, "36")
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT_COUPON, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val response = Gson().fromJson(result, BindTicketResponse::class.java)
                    _bindTicket.postValue(response.data)
                }
            }, getAccessToken())
    }

    /**
     * 扫码绑券
     */
    fun scanCodeBindTicketB(qrCode: String) {
        val request = BindTicketRequest(qrCode)
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(request), UrlConstants.API_ACCOUNT_COUPON, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    _bindTicketB.postValue(true)
                }
            }, getAccessToken())
    }

    /**
     * 获取订单数量和待支付数量
     */
    fun getOrderCount() {
        val map = HashMap<String, String>()
        OkhttpUtils.getInstance()
            .postRequest(Gson().toJson(map), UrlConstants.API_ORDER_COUNT, object :
                HttpCallBack {
                override fun onError(errorLog: String) {
                    _error.postValue(errorLog)
                }

                override fun onSuccess(result: String) {
                    val response = Gson().fromJson(result, OrderCountResponse::class.java)
                    // 存储数据
                    _orderCountData.postValue(response.data)
                }
            }, getAccessToken())
    }
}